package artid.com.arttogo.adapter;

import java.util.ArrayList;
import java.util.List;
import android.R.anim;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.VolleyError;
import artid.com.arttogo.R;
import artid.com.arttogo.activity.AddAddressActivity;
import artid.com.arttogo.bean.AddressList;
import artid.com.arttogo.bean.DefaultAddress;
import artid.com.arttogo.bean.DeleteAddress;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.task.CallbackSky;

/**
 * 地址适配器
 */
public class AddressListAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater mInflater;
    ViewHolder holder;
    CallbackSky cs;
    private List<AddressList> mList;
    int oldDef;// 设置默认地址之前的默认tag

    public AddressListAdapter(Context mContext, List<AddressList> bean) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        this.mList = bean;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_address_list, null);
            // vh.iv = (ImageView) convertView
            // .findViewById(R.id.iv_auction_img);
            vh.def = (View) convertView.findViewById(R.id.address_view_default);
            vh.del = (View) convertView.findViewById(R.id.address_view_del);
            vh.edit = (View) convertView.findViewById(R.id.address_view_edit);
            vh.name = (TextView) convertView
                    .findViewById(R.id.item_adress_name);
            vh.phone = (TextView) convertView
                    .findViewById(R.id.item_adress_phone);
            vh.address = (TextView) convertView
                    .findViewById(R.id.item_adress_addres);
            vh.checked = (ImageView) convertView
                    .findViewById(R.id.image_address_click);
            vh.uncheck = (ImageView) convertView
                    .findViewById(R.id.image_address);
            vh.deftext = (TextView) convertView.findViewById(R.id.text_def);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        final AddressList item = mList.get(position);

        vh.name.setText(item.getTrue_name());
        vh.phone.setText(item.getMob_phone());
        vh.address.setText(item.getAddressinfo());
        if (item.getIs_default().equals("1")) {
            oldDef = position;
            vh.checked.setVisibility(View.VISIBLE);
            vh.uncheck.setVisibility(View.GONE);
            vh.deftext.setTextColor(mContext.getResources().getColor(
                    R.color.btn_orange));
            vh.del.setVisibility(View.INVISIBLE);
            convertView.setOnClickListener(null);
            vh.def.setOnClickListener(null);

        } else {
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    reqDef(item.getAddress_id(), position);
                }
            });
            vh.del.setVisibility(View.VISIBLE);
            vh.checked.setVisibility(View.GONE);
            vh.uncheck.setVisibility(View.VISIBLE);
            vh.deftext.setTextColor(Color.parseColor("#333333"));
            vh.def.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    reqDef(item.getAddress_id(), position);
                }
            });
        }


        vh.del.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                reqRemove(item.getAddress_id(), position);
            }
        });
        vh.edit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mContext,AddAddressActivity.class);
                intent.putExtra("item", item);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    void reqDef(String id, final int index) {
        VolleyBasic.getInstance((Activity) mContext).post(Cache.defaultAddress,
                VolleyMap.getInstance().defaultAddress(id, "1"),
                DefaultAddress.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub

                        DefaultAddress item = (DefaultAddress) vd.getData();
                        if (item.getState()==1) {
                            AddressList addressitem = mList.get(index);
                            addressitem.setIs_default("1");
                            mList.set(index, addressitem);

                            AddressList olditem = mList.get(oldDef);
                            olditem.setIs_default("0");
                            mList.set(oldDef, olditem);
                            notifyDataSetChanged();
                        }

                        return;

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    void reqRemove(String id, final int index) {
        VolleyBasic.getInstance((Activity) mContext).post(Cache.deleteAddress,
                VolleyMap.getInstance().deleteAddress(id), DeleteAddress.class,
                new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub

                        DeleteAddress item = (DeleteAddress) vd.getData();
                        if (item.getState()==1) {
                            mList.remove(index);
                            notifyDataSetChanged();
                            if (mList.isEmpty()) {
                                Intent intent2 =new Intent("com.ChoiceAddressActivity.receiver");
                                intent2.putExtra("empt", true);
                                mContext.sendBroadcast(intent2);
                            }
                        }

                        return;

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    class ViewHolder {
        View def, edit, del;
        ImageView checked, uncheck;
        TextView name, phone, address, deftext;
    }
}
